<template>
  <div>
    <el-row type="flex" justify="end">
      <el-col :span="2">
        <el-button size="mini" @click="add" icon="el-icon-circle-plus" type="primary"
          >新增</el-button
        >
      </el-col>
    </el-row>
    <el-row>
      <el-col>
        <my-table
          :hasBorder="true"
          :hasIndex="true"
          :hasPagination="true"
          :hasStripe="true"
          :hasSelection="false"
          :tableColumnOptions="tableColumnOptions"
          :tableData="tableData"
        >
          <template #utilsCategory="{ data }">
            {{ leibie[data.utilsCategory] }}
          </template>
          <template #utilsFunctions="{ data }">
            {{ danwei[data.utilsFunctions] }}
          </template>
          <template #operation="{ data }">
            <el-button type="text" @click="update(data)">编辑</el-button>
            <el-button type="text" @click="remove(data)">删除</el-button>
          </template>
        </my-table>
      </el-col>
    </el-row>
    <my-dialog :dialogVisible.sync="infoDialog" :title="infoTitle">
      <template #content>
        <my-form
          :form="isAddInfo ? copyForm : form"
          :formOptions="infoFormOptions"
          :rules="infoRules"
          @sureSubmit="infoFormSubmit"
          ref="infoForm"
        >
          <template #utilsLeader="{ data }">
            <el-autocomplete
              v-model="test1"
              :fetch-suggestions="querySearch"
              placeholder="请输入内容"
              @select="handleSelect_person"
            ></el-autocomplete>
          </template>
        </my-form>
      </template>
    </my-dialog>
  </div>
</template>

<script>
import myForm from "@/components/myForm.vue";
import myTable from "@/components/myTable.vue";
import myDialog from "@/components/myDialog.vue";
import {getLabelByModel,projectCate,getModelByLabel} from "@/utils/metaInfo"
import {
  A_tableData,
  A_add,
  A_update,
  A_remove,
} from "@/api/infoMange/researchUnits";
import { A_getUserList } from "@/utils/aboutUsers";
import cloneDeep from "lodash/cloneDeep";
import { getSlide } from "@/api/user/myInfo";
export default {
  name: "researchUnits",
  inject: ["reload"],
  data() {
    return {
      dissiplineOptions:[],
      findFormOptions: [
        { type: "input", label: "单位名称", model: "utilsName" },
      ],
      tableColumnOptions: [
        {
          label: "单位名称",
          prop: "utilsName",
          width: "150",
        },
        {
          label: "单位号",
          prop: "utilsId",
          width: "150",
        },

         {
          label: "负责人",
          prop: "name",
          width: "150",
        },
        {
          label: "负责人工号",
          prop: "utilsLeader",
          width: "150",
        },
        {
          label: "类别",
          isSlot: true,
          slotName: "utilsCategory",
          prop: "utilsCategory",
          width: "150",
        },
        {
          label: "单位性质",
          isSlot: true,
          slotName: "utilsFunctions",
          prop: "utilsFunctions",
          width: "150",
        },
        {
          label: "操作",
          width: "180",
          isSlot: true,
          slotName: "operation",
          fixed: "right",
        },
      ],
      tableData: [],
      infoDialog: false,
      infoTitle: "编辑操作",
      isAddInfo: false,
      form: {},
      copyForm: {},
      infoFormOptions: [
        {
          type: "radio",
          label: "类别",
          width: 500,
          model: "utilsCategory",
          options: [
            {
              label: "自然科学",
              model: 0,
            },
            {
              label: "人文社科",
              model: 1,
            },
          ],
        },
        {
          type: "input",
          label: "单位名称",
          model: "utilsName",
          // options:this.dissiplineOptions
        },
        {
          type: "number",
          label: "单位号",
          model: "utilsId",
        },
        {
          type: "slot",
          slotName: "utilsLeader",
          label: "单位负责人",
          model: "utilsLeader",
        },
        {
          type: "radio",
          label: "单位性质",
          model: "utilsFunctions",
          options: [
            {
              label: "院系",
              model: 0,
            },
            {
              label: "职能部门",
              model: 1,
            },
          ],
        },
      ],
      infoRules: {
        utilsName: [{ required: true, message: "请输入单位名称" }],
        utilsId: [{ required: false, message: "请输入单位号" }],
        utilsLeader: [
          { required: false, message: "请输入负责人", trigger: "input" },
        ],
        utilsCategory: [{ required: true, message: "请选择类别" }],
        utilsFunctions: [{ required: false, message: "请选择单位性质" }],
      },
      danwei: ["院系", "职能部门"],
      leibie: ["自然科学", "人文社科", "管理部门"],
      test1: "",
      user_list: null,
    };
  },
  components: {
    myForm,
    myTable,
    myDialog,
  },
  async created() {
    this.getTableData();
    let res = await getSlide();
    let arr= [];
    res.data.sciDiscipline.forEach((item) => {
        let { subName, subKey } = item;
        arr.push({ label: subName, model: subKey });
      });
    this.dissiplineOptions = arr
    this.infoFormOptions[1].options = arr
    console.log(this.dissiplineOptions)
  },
  methods: {
    async getUserList() {
      let result = await A_getUserList();
      result.forEach((item) => {
        const value = item.userId + " : " + item.userName;
        item.value = value;
      });
      this.user_list = result;
      // console.log(this.user_list)
    },
    async findTableData(data) {
      let result = await A_tableData(data);
      if (result.code === 1) {
        this.tableData = cloneDeep(result.data);
      } else {
        this.$message({ type: "error", message: "信息获取失败" });
      }
    },
    async getTableData() {
      let result = await A_tableData({});
      if (result.code === 1) {
        this.tableData = cloneDeep(result.data);
      } else {
        this.$message({ type: "error", message: "信息获取失败" });
      }
    },
    async reset() {
      let result = await A_tableData({});
      if (result.code === 1) {
        this.tableData = cloneDeep(result.data);
      } else {
        this.$message({ type: "error", message: "信息获取失败" });
      }
    },
    add() {
      this.isAddInfo = true;
      this.infoDialog = true;

      this.getUserList();
      
    },
    update(data) {
      
      this.isAddInfo = false;
      this.infoDialog = true;
      this.form = cloneDeep(data);
      console.log(data)
      console.log(this.form)
      let options = [
            {
              label: "院系",
              model: 0,
            },
            {
              label: "职能部门",
              model: 1,
            },
          ]
      this.form.utilsCategory = getLabelByModel(this.form.utilsCategory,projectCate)
      this.form.utilsFunctions = getLabelByModel(this.form.utilsFunctions,options)
      this.getUserList();
    },
    remove(data) {
      this.$confirm("你确定要删除该信息吗？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          const { utilsKey } = data;
          let res = await A_remove(utilsKey);
          if (res.code === 1) {
            this.$message({ type: "success", message: res.data });
            this.reload();
          } else {
            this.$message({ type: "error", message: res.msg });
          }
        })
        .catch(() => {
          // this.$message({
          //   type: "info",
          //   message: "已取消删除",
          // });
        });
    },
    async infoFormSubmit(data) {
      console.log(data)
      data.utilsCategory = getModelByLabel(data.utilsCategory,projectCate)
      let options =  [
            {
              label: "院系",
              model: 0,
            },
            {
              label: "职能部门",
              model: 1,
            },
          ]
      data.utilsFunctions = getModelByLabel(data.utilsFunctions,options)
      console.log(this.form)
      if (this.isAddInfo) {
        let res = await A_add(data);
        if (res.code === 1) {
          this.$message({ type: "success", message: res.data });
          this.reload();
        } else {
          this.$message({ type: "error", message: res.msg });
        }
      } else {
        let res = await A_update(data);
        if (res.code === 1) {
          this.$message({ type: "success", message: res.data });
          this.reload();
        } else {
          this.$message({ type: "error", message: res.msg });
        }
      }
      // this.$refs.filesUpload.submit();
    },
    querySearch(queryString, cb) {
      var user_list = this.user_list;
      var results = queryString
        ? user_list.filter(this.createFilter(queryString))
        : user_list;
      // 调用 callback 返回建议列表的数据
      cb(results);
    },
    createFilter(queryString) {
      return (user_list) => {
        return (
          user_list.userName.indexOf(queryString) === 0
        );
      };
    },
    handleSelect_person(item) {
      this.$refs.infoForm.form.utilsLeader = item.userId;
    },
  },
  computed: {},
};
</script>

<style scoped>
.upload-demo {
  display: inline-block;
  margin: 0 3px;
}
</style>